/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.productBrand.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.util.excel.ExportExcel;
import com.seeyoui.kensite.common.base.domain.Page;

import com.alibaba.fastjson.JSONPath;
import com.google.gson.Gson;
import com.seeyoui.kensite.bussiness.orderlang.productBrand.domain.ProductBrand;
import com.seeyoui.kensite.bussiness.orderlang.productBrand.service.ProductBrandService;

/**
 * ProductBrand
 * 
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "productBrand")
public class ProductBrandController extends BaseController {

    @Autowired
    private ProductBrandService productBrandService;

    /**
     * 展示列表页面
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:view")
    @RequestMapping(value = "/{page}")
    public ModelAndView view(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            @PathVariable String page) throws Exception {
        return new ModelAndView("bussiness/orderlang/productBrand/" + page, modelMap);
    }

    /**
     * 根据ID查询单条数据
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:select")
    @RequestMapping(value = "/data/{id}")
    @ResponseBody
    public Object data(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, @PathVariable String id)
            throws Exception {
        ProductBrand productBrand = productBrandService.findOne(id);
        return productBrand;
    }

    /**
     * 根据ID查询单条数据并返回相应表单
     * 
     * @param modelMap
     * @param module
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:view")
    @RequestMapping(value = "/form/{page}/{id}")
    public ModelAndView form(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
            @PathVariable String page, @PathVariable String id) throws Exception {
        ProductBrand productBrand = productBrandService.findOne(id);
        modelMap.put("productBrand", productBrand);
        return new ModelAndView("bussiness/orderlang/productBrand/" + page, modelMap);
    }

    /**
     * 获取列表展示数据
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:select")
    @RequestMapping(value = "/list/data", method = RequestMethod.POST)
    @ResponseBody
    public Object listData(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        productBrand.setDelFlag("0");
        List<ProductBrand> productBrandList = productBrandService.findList(productBrand);
        int total = productBrandService.findTotal(productBrand);
        EasyUIDataGrid eudg = new EasyUIDataGrid();
        eudg.setTotal(String.valueOf(total));
        eudg.setRows(productBrandList);
        return eudg;
    }

    /**
     * 获取列表展示数据带分页器用于jsp自己做分页
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:view")
    @RequestMapping(value = "/list/page", method = RequestMethod.POST)
    @ResponseBody
    public Object listPage(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        productBrand.setDelFlag("0");
        List<ProductBrand> productBrandList = productBrandService.findList(productBrand);
        int total = productBrandService.findTotal(productBrand);
        Page<ProductBrand> page = new Page<ProductBrand>(productBrand.getPage(), productBrand.getRows(), total);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("rows", productBrandList);
        dataMap.put("page", page);
        return dataMap;
    }

    /**
     * 获取所有数据
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:select")
    @RequestMapping(value = "/list/all", method = RequestMethod.POST)
    @ResponseBody
    public Object listAll(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        productBrand.setDelFlag("0");
        List<ProductBrand> productBrandList = productBrandService.findAll(productBrand);
        return productBrandList;
    }

    /**
     * 保存新增的数据
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:insert")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        if (!beanValidator(modelMap, productBrand)) {
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        // 查询当前品牌名称是否存在
        productBrand.setDelFlag("0");
        List<ProductBrand> productBrands = productBrandService.findList(productBrand);
        if (productBrands != null && productBrands.size() > 0) {
            modelMap.put("message", "品牌名称已经存在");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        productBrandService.save(productBrand);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 保存修改的数据
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        if (!beanValidator(modelMap, productBrand)) {
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
        ProductBrand productBrandOne = productBrandService.findOne(productBrand.getId());
        productBrandOne.setBrandName(productBrand.getBrandName());
        productBrandService.update(productBrandOne);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 删除数据库
     * 
     * @param modelMap
     * @param productBrandId
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, String id)
            throws Exception {
        List<String> listId = Arrays.asList(id.split(","));
        productBrandService.delete(listId);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 假删除
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public String remove(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        productBrandService.remove(productBrand);
        RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        return null;
    }

    /**
     * 导出Excel数据
     * 
     * @param modelMap
     * @param productBrand
     * @return
     * @throws Exception
     */
    // @RequiresPermissions("productBrand:productBrand:export")
    @RequestMapping(value = "/export")
    public String export(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap, ProductBrand productBrand)
            throws Exception {
        String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        List<ProductBrand> productBrandList = productBrandService.findAll(productBrand);
        new ExportExcel(null, ProductBrand.class).setDataList(productBrandList).write(response, fileName).dispose();
        return null;
    }

    /**
     * 添加一条商品品牌
     * 
     * @param request
     * @param response
     * @return
     */

    // 请求数据格式 {"prodBrandJson":{"brandName":"狗","seq":"1"}}
    @RequestMapping(value = "/addProdBrand", method = RequestMethod.POST)
    @ResponseBody
    public Result addProdBrand(HttpServletRequest request, HttpServletResponse response) {
        String json = request.getParameter("prodBrandJson");

        if (json != null && !json.equals("")) {
            ProductBrand prodBrand = new Gson().fromJson(json, ProductBrand.class);
            if (prodBrand != null) {
                productBrandService.save(prodBrand);
                return Result.success(null, "商品品牌添加成功");
            } else {
                return Result.failure("添加失败");
            }
        }
        return null;
    }

    /**
     * 删除一条 商品品牌记录
     * 
     * @param request
     * @param response
     * @return
     */
    // 请求数据格式 {"id":"1"}
    @RequestMapping(value = "/deleteProdBrand", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteProdBrand(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        if (id != null && !id.equals("")) {
            List<String> listId = Arrays.asList(id.split(","));
            productBrandService.delete(listId);
            return Result.success(null, "删除成功");
        } else {
            return Result.failure("删除失败");
        }
    }

    /**
     * 修改商品品牌
     * 
     * @param request
     * @param response
     * @param brandId
     * @return
     */
    // 请求数据格式{"prodBrandJson":{"id":"1","brandName":"狗","seq":"1"}}
    @RequestMapping(value = "/updateProdBrand", method = RequestMethod.POST)
    @ResponseBody
    public Result updateProdBrand(HttpServletRequest request, HttpServletResponse response) {
        String json = request.getParameter("prodBrandJson");

        if (json != null && !json.equals("")) {
            String id = (String) JSONPath.read(json, "$.id");
            ProductBrand prodBrand = productBrandService.findOne(id);
            prodBrand.setBrandName((String) JSONPath.read(json, "$.brandName"));
            prodBrand.setSeq((String) JSONPath.read(json, "$.seq"));
            productBrandService.update(prodBrand);
            return Result.success(null, "修改成功");
        } else {
            return Result.failure("修改失败");
        }
    }

    /**
     * 查询所有商品品牌数据
     * 
     * @param request
     * @param response
     * @param pBrand
     * @return
     */
    @RequestMapping(value = "/findAllProdBrand", method = RequestMethod.POST)
    @ResponseBody
    public Result findAllProdBrand(HttpServletRequest request, HttpServletResponse response, ProductBrand pBrand) {
    	pBrand.setDelFlag("0");
        List<ProductBrand> brands = productBrandService.findAll(pBrand);
        if (brands != null && brands.size() > 0) {
            return Result.success(brands, "查询成功");
        } else {
            return Result.failure("查詢失敗");

        }
    }

}